package com.wxzz.elearning.common.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileUtil {

    public static File toFile(MultipartFile multipartFile) {
        // 获取文件名
        String fileName = multipartFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若需要防止生成的临时文件重复,可以在文件名后添加随机码

        try {
            File file = File.createTempFile(fileName, prefix);
            multipartFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
/*
    // 将MultipartFile file转换成为File
	        File f_file = MultipartFileToFile(file);
	        String path = f_file.getCanonicalPath();
	        long duration_new = VideoUtil.getDuration(path);
	        duration = (int)duration_new;  // 获取的视频时长
	        f_file.delete();
 */
    }

    public static void deleteFile(File file){
        File f = new File(file.toURI());
        if (f.delete()){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }
}
